package com.yikaiye.android.yikaiye.ui.activity.mine;

import static com.scwang.smartrefresh.layout.util.SmartUtil.dp2px;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.databinding.ActivitySearchCompanyBinding;
import com.yikaiye.android.yikaiye.ui.activity.order.viewmodel.MyCompanyViewModel;
import com.yikaiye.android.yikaiye.ui.adapter.MyCompanyAdapter;
import com.yikaiye.android.yikaiye.ui.adapter.SearchCompanyAdapter;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.SearchHistoryUtil;
import com.yikaiye.android.yikaiye.utils.ToastHelper;
import com.yikaiye.android.yikaiye.widget.flowLayout.FlowLayoutManager;
import com.yikaiye.android.yikaiye.widget.flowLayout.SpaceItemDecoration;

import java.util.List;

/**
 * 搜索我的企业
 */
public class SearchCompanyActivity extends BaseActivity<ActivitySearchCompanyBinding, MyCompanyViewModel> {

    private MyCompanyAdapter companyAdapter;
    private SearchCompanyAdapter searchCompanyAdapter;


    @Override
    protected MyCompanyViewModel bindModel() {
        return getViewModel(this, MyCompanyViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.my_company));
        companyAdapter = new MyCompanyAdapter(context);
        binding.recyclerView.setAdapter(companyAdapter);
        companyAdapter.setEmptyViewEnable(true);
        companyAdapter.setEmptyViewLayout(context, R.layout.layout_empty_list);

        binding.historyRv.addItemDecoration(new SpaceItemDecoration(dp2px(5)));
        binding.historyRv.setLayoutManager(new FlowLayoutManager());
        searchCompanyAdapter = new SearchCompanyAdapter();
        binding.historyRv.setAdapter(searchCompanyAdapter);
        initHistory();
    }

    @Override
    protected void initClick() {
        viewModel.companyRecordsModelMutableLiveData.observe(this, companyModels -> {
            companyAdapter.submitList(companyModels.getRecords());
            CommonUtil.closeKeyboard(this);
        });
        viewModel.mErrorData.observe(this, ToastHelper::show);

        binding.etSearchContent.setOnEditorActionListener((textView, i, keyEvent) -> {
            CommonUtil.closeKeyboard(this);
            String keyWord = binding.etSearchContent.getText().toString();
            if (!TextUtils.isEmpty(keyWord)) {
                searchCompany(keyWord);
            } else {
                ToastHelper.show(getString(R.string.input_company_name_or_credit_no));
            }
            return true;
        });

        binding.etSearchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (TextUtils.isEmpty(charSequence.toString())) {
                    binding.searchHistoryLl.setVisibility(View.VISIBLE);
                }
                binding.clearIv.setVisibility(charSequence.toString().isEmpty() ? View.GONE : View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        viewModel.onDelayClick(binding.clearIv, o -> {
            binding.searchHistoryLl.setVisibility(View.VISIBLE);
            binding.etSearchContent.setText("");
            initHistory();
        });

        viewModel.onDelayClick(binding.deleteIv, o -> {
            SearchHistoryUtil.cleanCompanyHistory();
            initHistory();
        });

        searchCompanyAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> searchCompany(searchCompanyAdapter.getItem(i)));
    }

    /**
     * 搜索企业
     */
    private void searchCompany(String keyword) {
        SearchHistoryUtil.saveCompanyKeyWord(keyword);
        binding.searchHistoryLl.setVisibility(View.GONE);
        viewModel.getMyCompanyList(1, keyword);
    }

    /**
     * 初始化搜索历史
     */
    private void initHistory() {
        List<String> historyData = SearchHistoryUtil.getCompanyHistory();
        searchCompanyAdapter.submitList(historyData);
    }
}